/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.composites;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.jface.text.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.InstanceSpecification;

import com.cea.papyrus.core.commands.MoveInListCommand;
import com.cea.papyrus.core.editor.CommandStackUtils;
import com.cea.papyrus.core.ui.dialogs.ChooseSetClassifiersDialog;
import com.cea.papyrus.umlutils.Element;

/**
 * 
 */
public class InstanceSpecificationClassifiersComposite extends DecoratedTableComposite implements ISectionComposite {

	/**
	 * Default Constructor.
	 */
	public InstanceSpecificationClassifiersComposite() {
		super("Classifiers");
	}

	/**
	 * key to access UML Data from table items.
	 */ 
	private final static String KEY = "instanceSpecClassifiers_data";
	
	/**
	 * Operation for which methods are displayed.
	 */
	private InstanceSpecification umlInstanceSpecification;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#setElement(com.cea.utils.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 */
	@Override
	public void setElement(Element element) {
		super.setElement(element);
		this.umlInstanceSpecification = (InstanceSpecification)element.getUml2Element();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#refresh()
	 */
	@Override
	public void refresh() {
		if( !table.isDisposed()&& (umlInstanceSpecification!=null)){
			// Remove parameters from table 
			table.removeAll();

			// Read and add parameter
			Iterator<Classifier> it = umlInstanceSpecification.getClassifiers().iterator();
			while (it.hasNext()) {
				Classifier classifier = it.next();
				TableItem item = new TableItem(table, SWT.BORDER);
				// choose the icon corresponding to the instance of the classifier
				
				//item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(classifier));
				item.setImage(getLabelProvider().getImage(classifier));
				item.setText(getLabelProvider().getText(classifier));
				item.setData(KEY, classifier);
				
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#addButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void addButtonPressed() {
		ChooseSetClassifiersDialog dialog = new ChooseSetClassifiersDialog(composite.getShell(), getElement().getUml2Element());
		dialog.create();
		dialog.setFocus();
		final int result = dialog.open();
		if(dialog.hasElementChanged()) {
			refresh();
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#downButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void downButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();

		for(int i = (tableItems.length-1); i >= 0; i--) {
			// get use cases list
			List classifiers = umlInstanceSpecification.getClassifiers();
			// get use case
			Classifier classifier = (Classifier) tableItems[i].getData(KEY);
			int newIndex = table.indexOf(tableItems[i]) + 1;
			if(newIndex < classifiers.size()) {
				//new MoveInListCommand(parameters, parameter, newIndex).execute();
				compoundCommand.add(new MoveInListCommand(classifiers, classifier, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#editItem(org.eclipse.swt.widgets.TableItem)
	 */
	/**
	 * 
	 * 
	 * @param item 
	 */
	@Override
	public void editItem(TableItem item) {
		if (item == null) {
			return;
		}
		// e linked to the item
		Classifier classifier = (Classifier)item.getData(KEY);

		// TODO edit a classifier
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#removeButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void removeButtonPressed() {
		// get selection and remove it
		TableItem[] tableItems = table.getSelection();
		for(int i = 0; i < tableItems.length ; i++) {
			// get use case remove umlClassifier from its subject list 
			// and removed it from the use case list
			Classifier classifier = (Classifier) tableItems[i].getData(KEY);
			umlInstanceSpecification.getClassifiers().remove(classifier);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#upButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void upButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();
		// get extension point list
		List classifiers = umlInstanceSpecification.getClassifiers();
		// get extension point
		for(int i = 0; i < tableItems.length ; i++) {
			// get use case
			Classifier classifier = (Classifier) tableItems[i].getData(KEY);
			int newIndex = table.indexOf(tableItems[i]) - 1;
			if (newIndex >= 0) {
				compoundCommand.add(new MoveInListCommand(classifiers, classifier, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);
	}
}
